package com.qianli.cashmama.trade.integration.notification.message.utils;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApplicationException;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.fqgj.xjd.user.client.response.UserAccessInfo;
import com.qianli.cashmama.product.facade.AppFacade;
import com.qianli.cashmama.product.facade.result.AppPackageInfo;
import com.qianli.cashmama.trade.common.enums.message.MessageBizCodeEnum;
import com.qianli.cashmama.trade.common.enums.message.MessageSceneEnum;
import com.qianli.cashmama.trade.common.utils.TradeConfigUtil;
import com.qianli.cashmama.trade.dao.TTradeExtraDao;
import com.qianli.cashmama.trade.entity.TTradeExtraEntity;
import com.qianli.cashmama.trade.integration.user.UserIntegrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 马甲包 消息工具类
 *
 * @author moss
 * 2019-04-02
 */
@Component
public class MessageUtils {
    public static final Log LOGGER = LogFactory.getLog(MessageUtils.class);

    @Autowired
    TradeConfigUtil apolloConfigUtil;
    @Autowired
    UserIntegrationService userIntegrationService;

    @Autowired
    private AppFacade appFacade;

    @Autowired
    private TTradeExtraDao tTradeExtraDao;

    /**
     * 查询用户下单app包信息
     *
     * @param userCode
     * @param tradeNo
     * @return
     */
    public AppPackageInfo getAppPackageInfoByUserCode(String userCode, String tradeNo) {
        try {
            String packageName = null;
            if (StringUtils.isEmpty(tradeNo)) {
                UserAccessInfo userAccessInfo = userIntegrationService.getUserAccessInfoByUserCode(userCode);
                packageName = userAccessInfo.getPackageName();
            } else {
                TTradeExtraEntity tTradeExtraEntity = tTradeExtraDao.selectByTradeNo(tradeNo);
                if (tTradeExtraEntity != null && StringUtils.isNotEmpty(tTradeExtraEntity.getPackageName())) {
                    packageName = tTradeExtraEntity.getPackageName();
                } else {
                    UserAccessInfo userAccessInfo = userIntegrationService.getUserAccessInfoByUserCode(userCode);
                    packageName = userAccessInfo.getPackageName();
                }
            }
            AppPackageInfo appPackageInfo = appFacade.getAppPackageInfoByPackageName(packageName).getData();
            LOGGER.info("调试日志1,userCode:{},tradeNo:{},packageName:{},appPackageInfo:{}", userCode, tradeNo, packageName, JSON.toJSONString(appPackageInfo));
            return appPackageInfo;
        } catch (Exception e) {
            LOGGER.error("查询失败,userCode:{}", userCode, e);
            throw new ApplicationException("查询App信息失败");
        }
    }

    /**
     * 根据用户code 和 消息场景确定 消息bizCode
     */
    public String getMessageBizCodeByPackageName(String userCode, String tradeNo, MessageSceneEnum messageSceneEnum) {
        AppPackageInfo appPackageInfo = this.getAppPackageInfoByUserCode(userCode, tradeNo);

        String appCode = appPackageInfo.getAppCode();
        String appName = appPackageInfo.getAppName();
        String packageName = appPackageInfo.getPackageName();

        //已经在运行的app 还是按照appName获取场景码
        String messageBizCode;
        if ("in.cashmama.app".equals(packageName) || "in.dhanadhan.loan.money.cash".equals(packageName) || "in.credfox.lending".equals(packageName)
                || "35".equals(appCode) || "36".equals(appCode)) {
            messageBizCode = MessageBizCodeEnum.getMessageBizCodeEnum(appName, messageSceneEnum);
        } else {
            messageBizCode = MessageBizCodeEnum.getMessageBizCodeByPackageName(packageName, messageSceneEnum);
        }
        LOGGER.info("trade获取场景码,userCode:{},tradeNo:{},packageName:{},messageBizCode:{}", userCode, tradeNo, packageName, messageBizCode);
        return messageBizCode;
    }

    /**
     * 获取邮件模板
     */
    public String getEmailBasicTemplateUrl(String userCode, String tradeNo) {
//        AppPackageInfo appPackageInfo = this.getAppPackageInfoByUserCode(userCode, tradeNo);
//
//        String appCode = appPackageInfo.getAppCode();
//        String appName = appPackageInfo.getAppName();
//        String packageName = appPackageInfo.getPackageName();
//
//        //已经在运行的app 还是按照appName获取场景码
//        String key = null;
//        if ("in.cashmama.app".equals(packageName) || "in.dhanadhan.loan.money.cash".equals(packageName) || "in.credfox.lending".equals(packageName)
//                || "35".equals(appCode) || "36".equals(appCode)) {
//            key = String.format("%s.mail.basic.template.url", appName.toLowerCase());
//        } else {
//            key = String.format("%s.mail.basic.template.url", packageName.replaceAll("\\.", "").toLowerCase());
//        }
//        String template = config.getProperty(key, "https://cashmama-pro.oss-ap-south-1.aliyuncs.com/email/basicTemplate.html");
//        LOGGER.info("trade获取邮件模板,userCode:{},tradeNo:{},key:{},template:{}", userCode, tradeNo, key, template);
//        return template;
        return null;
    }

    /**
     * 获取邮件模板02
     */
    public String getEmailBasicTemplate02Url(String userCode, String tradeNo) {
//        AppPackageInfo appPackageInfo = this.getAppPackageInfoByUserCode(userCode, tradeNo);
//
//        String appCode = appPackageInfo.getAppCode();
//        String appName = appPackageInfo.getAppName();
//        String packageName = appPackageInfo.getPackageName();
//
//        //已经在运行的app 还是按照appName获取场景码
//        String key = null;
//        if ("in.cashmama.app".equals(packageName) || "in.dhanadhan.loan.money.cash".equals(packageName) || "in.credfox.lending".equals(packageName)
//                || "35".equals(appCode) || "36".equals(appCode)) {
//            key = String.format("%s.mail.basic.template_2.url", appName.toLowerCase());
//        } else {
//            key = String.format("%s.mail.basic.template_2.url", packageName.replaceAll("\\.", "").toLowerCase());
//        }
//        String template = config.getProperty(key, "https://cashmama-pro.oss-ap-south-1.aliyuncs.com/email/basicTemplate_2.html");
//        LOGGER.info("trade获取邮件模板2,userCode:{},tradeNo:{},key:{},template:{}", userCode, tradeNo, key, template);
        return null;
    }


}
